<?php
// +----------------------------------------------------------------------
// | 奇乐中介担保交易系统 免费版 2019
// +----------------------------------------------------------------------
// | 官方网址：http://www.qilecms.com   
// +----------------------------------------------------------------------
// | 产品文档：http://www.qilecms.com/doc
// +----------------------------------------------------------------------
// | 开发作者:奇乐CMS
// +----------------------------------------------------------------------
// | 版权说明：这不是一个自由软件！本产品仅限个人使用，但请务必保留底部版权，以免产生不必要的纠纷,
// | 您只能在不用于商业目的的前提下对程序代码进行修改和使用，
// | 任何企业和个人不允许对程序代码以任何形式任何目的再发布。
// +----------------------------------------------------------------------
namespace app\user\controller;
use think\Db;
class Rights extends Base
{
   public function initialize()
    {
       parent::initialize();
       $this->isLogin();
       $this->isValidate();
    }
//申请维权
  public function index(){
    
    if($this->request->isAjax()){
       $param = $this->request->param();
      //表单验证
        $rule =[
          
          'type'=>'require',
          'content'=>'require',
          'from'=>'require',
          'to'=>'require',
          'oid'=>'require',
        ];
        $msg= [
         'type.require' =>'请选择维权类型',
         'content.require' =>'请输入具体原因', 
         'from.require' =>'缺少参数',
         'to.require' =>'缺少参数',
         'oid.require' =>'缺少参数',
        ];
         $error = $this->checkSubmit($param,$rule,$msg);  
         //调用common 公共控制器中的 checkSubmit 方法
         if($error){
              return error_json($error);
         }
       
       if($param['__token__'] != session('__token__')){
            return error_json('请勿重复提交');
       }  
       $data['order_id'] = intval($param['oid']);
       $data['info_id'] = intval($param['info_id']);
   //获得订单标题
       $order = model('guarantee')->infoData(['id'=>intval($param['oid'])]);
       $data['title']     = $order['title'];
       $data['type']      = $param['type'];
       $data['from_uid']  = $param['from'];
       $data['to_uid']    = $param['to'];
       $data['content']   = $param['content'];
       $data['image_url'] = $param['image_url'];

       $data['create_time'] = time();


       model('guaranteeRights')->addData($data);
    //发送消息
      model('message')->send($data['to_uid'],'新仲裁消息','您的订单编号：'.$data['order_id'].'<br>对方申请了仲裁，该订单交易已冻结等待客服处理！'.'<a href="/order/'.$data['order_id'].'.html">查看订单</a>');

       session('__token__',null);

       return success_json('维权申请成功，等待客服介入');
    }else{
      $param = request()->param();

      if(empty($param['id'])){
        return false;
      }
      $id = $param['id'];
      $map['id'] = intval($id);
      $order = model('guarantee')->infoData($map);
      $this->assign('order',$order);
    	return $this->fetch();
    }
    
  }

  //我发起的维权

  public function myRights()
  {

   if($this->request->isAjax()){
      $param = $this->request->param();
     
       $statusType =  isset($param['status'])?intval($param['status']):'all'; 
      
       $limit = !empty($param['limit'])?intval($param['limit']):'10'; //限制数量
       $start = !empty($param['start'])?strtotime($param['start']):'';//开始
       $end   = !empty($param['end'])?strtotime($param['end']):''; //结束

         // 我发起的维权
         $where[] =['from_uid','=',$this->user->uid];

      
      //状态筛选
        switch($statusType)
        {
         case '0':
         // 待处理       
             $where[] = ['status','=',0];
         break;
         case '1':
         // 处理中        
             $where[] = ['status','=',1];
         break; 
         case '2':
         // 已处理          
             $where[] = ['status','=',2];
         break; 
         case '-1':
         // 未成立   
            $where[] = ['status','=',-1];
         break;

        }
        $order ='id desc';
     $count  = Db::name('guaranteeRights')->where($where)->count();
     $lists  = Db::name('guaranteeRights')->where($where)->order($order)->paginate($limit);
      $list  = $lists->toArray()['data'];
      $userlist = $this->getCache('user','uid');
      $data = [];
      foreach($list as $k=>$v){
         $data[$k]['create_time'] = date("Y-m-d H:i:s",time());
         $data[$k]['form_user']   = $userlist[$v['from_uid']]['username'];
         $data[$k]['to_user']     = $userlist[$v['to_uid']]['username'];
         $map['id'] =$v['order_id'];
         $order = model('guarantee')->infoData($map);
         $data[$k]['title'] = $v['title'];
         $data[$k]['content'] = $v['content'];
         $data[$k]['image_url'] = $v['image_url'];
         $data[$k]['status'] = $v['status'];
         $data[$k]['id'] = $v['id'];
         $data[$k]['order_id'] = $v['order_id'];



         $data[$k]['result'] = $v['result'];

      }

     return json(['code'=>0,'msg'=>'success','count'=>$count,'data'=>$data]);



}else{
     return $this->fetch();
}

  } 

//对方发起的维权  
  public function rightsToMe()
  {
   if($this->request->isAjax()){
      $param = $this->request->param();
     
       $statusType =  isset($param['status'])?intval($param['status']):'all'; 
      
       $limit = !empty($param['limit'])?intval($param['limit']):'10'; //限制数量
       $start = !empty($param['start'])?strtotime($param['start']):'';//开始
       $end   = !empty($param['end'])?strtotime($param['end']):''; //结束

         // 我收到的维权
         $where[] =['to_uid','=',$this->user->uid];

  
      
      //状态筛选
        switch($statusType)
        {
         case '0':
         // 待处理       
             $where[] = ['status','=',0];
         break;
         case '1':
         // 处理中        
             $where[] = ['status','=',1];
         break; 
         case '2':
         // 已处理          
             $where[] = ['status','=',2];
         break; 
         case '-1':
         // 未成立   
            $where[] = ['status','=',-1];
         break;

        }
        $order ='id desc';
     $count  = Db::name('guaranteeRights')->where($where)->count();
     $lists  = Db::name('guaranteeRights')->where($where)->order($order)->paginate($limit);
      $list  = $lists->toArray()['data'];
      $userlist = $this->getCache('user','uid');
      $data = [];
      foreach($list as $k=>$v){
         $data[$k]['create_time'] = date("Y-m-d H:i:s",time());
         $data[$k]['form_user']   = $userlist[$v['from_uid']]['username'];
         $data[$k]['to_user']     = $userlist[$v['to_uid']]['username'];
         $map['id'] =$v['order_id'];
         $order = model('guarantee')->infoData($map);
         $data[$k]['title'] = $v['title'];
         $data[$k]['content'] = $v['content'];
         $data[$k]['image_url'] = $v['image_url'];
         $data[$k]['status'] = $v['status'];
         $data[$k]['id'] = $v['id'];
         $data[$k]['order_id'] = $v['order_id'];



         $data[$k]['result'] = $v['result'];

      }

     return json(['code'=>0,'msg'=>'success','count'=>$count,'data'=>$data]);



}else{
     return $this->fetch();
}


  } 

// 会员中心维权记录
  public function rightsRecord()
  {
        if($this->request->isAjax()){
                     $param = $this->request->param();
                    
                      $type  = !empty($param['type'])?intval($param['type']):1; //发起类型
                      $statusType =  isset($param['status'])?intval($param['status']):'all'; 
                     
                      $limit = !empty($param['limit'])?intval($param['limit']):'10'; //限制数量
                      $start = !empty($param['start'])?strtotime($param['start']):'';//开始
                      $end   = !empty($param['end'])?strtotime($param['end']):''; //结束
               
                     if($type ==1){
                     	// 我发起的维权
                        $where[] =['from_uid','=',$this->user->uid];

                     }elseif($type ==2){
                     	// 我收到的维权
                        $where[] =['to_uid','=',$this->user->uid];

                     }
                     
                     //状态筛选
                       switch($statusType)
                       {
                        case '0':
                        // 待处理       
                            $where[] = ['status','=',0];
                        break;
                        case '1':
                        // 处理中        
                            $where[] = ['status','=',1];
                        break; 
                        case '2':
                        // 已处理          
                            $where[] = ['status','=',2];
                        break; 
                        case '-1':
                        // 未成立   
                           $where[] = ['status','=',-1];
                        break;

                       }
                 
                    $count  = Db::name('guaranteeRights')->where($where)->count();
                    $lists  = Db::name('guaranteeRights')->where($where)->order($order)->paginate($limit);
                     $list  = $lists->toArray()['data'];
                     $userlist = $this->getCache('user','uid');
                     $data = [];
                     foreach($list as $k=>$v){
                        $data[$k]['create_time'] = date("Y-m-d H:i:s",time());
                        $data[$k]['form_user']   = $userlist[$v['from_uid']]['username'];
                        $data[$k]['to_user']     = $userlist[$v['to_uid']]['username'];
                        $map['id'] =$v['order_id'];
                        $order = model('guarantee')->infoData($map);
                        $data[$k]['title'] = $v['title'];
                        $data[$k]['content'] = $v['content'];
                        $data[$k]['image_url'] = $v['image_url'];
                        $data[$k]['status'] = $v['status'];
                        $data[$k]['id'] = $v['id'];
                        $data[$k]['order_id'] = $v['order_id'];
       


                        $data[$k]['result'] = $v['result'];

                     }

                    return json(['code'=>0,'msg'=>'success','count'=>$count,'data'=>$data]);
       


        }else{
                    return $this->fetch();
        }
    
  }
}